package com.joplus.housekeeper.ui.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.joplus.housekeeper.R;


/**
 * Created by sc on 2015/10/29.
 */
public class LoadingDialog2 {


    private Activity mContext;


    private int[] iconList = {R.drawable.loadingicon_1, R.drawable.loadingicon_2, R.drawable.loadingicon_3, R.drawable.loadingicon_4, R.drawable.loadingicon_5, R.drawable.loadingicon_6, R.drawable.loadingicon_7, R.drawable.loadingicon_8, R.drawable.loadingicon_9, R.drawable.loadingicon_10, R.drawable.loadingicon_11, R.drawable.loadingicon_12, R.drawable.loadingicon_13, R.drawable.loadingicon_14, R.drawable.loadingicon_15, R.drawable.loadingicon_16, R.drawable.loadingicon_17, R.drawable.loadingicon_18, R.drawable.loadingicon_19, R.drawable.loadingicon_20};
    private final ImageView iv;
    private final Dialog dialog;


    public LoadingDialog2(Activity context) {
        this.mContext = context;
        View view = LayoutInflater.from(mContext).inflate(R.layout.progress2, null);
        dialog = new Dialog(mContext, R.style.no_background_dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        iv = (ImageView) view.findViewById(R.id.iv);
        iv.setImageResource(iconList[0]);

    }

    private int count = 0;

    public void show() {
        dialog.show();
        AnimationSet animation = (AnimationSet) AnimationUtils.loadAnimation(mContext, R.anim.load_anim);
        animation.setInterpolator(new AccelerateDecelerateInterpolator());

        animation.getAnimations().get(0).setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                count++;

                if (count == 20) {
                    count = 0;
                }
                iv.setImageResource(iconList[count]);
            }
        });

        iv.startAnimation(animation);


    }

    public void dismiss() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }


}
